package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.Util.DeSetUtil;
import com.hyb.Vo.OrderVo;
import com.hyb.entity.Commodity;
import com.hyb.entity.Order;
import com.hyb.mapper.CommodityMapper;
import com.hyb.mapper.OrderMapper;
import com.hyb.page.PageReq;
import com.hyb.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  订单Service接口实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public void createOrder(List<OrderVo> orderVoList) {
        OrderVo orderVo0 = orderVoList.get(0);

        Order order = new Order();
        order.setOrderState("已下单");
        order.setCustomerId(orderVo0.getCustomerId());
        orderMapper.insert(order);
        Integer order1 = orderMapper.selectlatelyOrder(orderVo0.getCustomerId());
        for (OrderVo orderVo : orderVoList) {
            Commodity commodity = new Commodity();
            commodity.setCommodityNum(orderVo.getCommodityNum());
            commodity.setOrderId(order1);
            commodity.setProductId(orderVo.getProductId());
            commodityMapper.insert(commodity);
        }
    }

    @Override
    public PageReq loadOrders(Integer limit, Integer current, Integer customerId) {
        Page<OrderVo> page = new Page<>(current,limit);
        PageReq pageReq = new PageReq();
        IPage<OrderVo> orders = orderMapper.selectOrders(page, customerId);
        pageReq.setTotal(orders.getTotal());
        pageReq.setMyData(orders.getRecords());
        return pageReq;
    }

    @Override
    public void updateOrder(List<OrderVo> orderVoList) {
        OrderVo orderVo = orderVoList.get(0);
        List<OrderVo> orderVos = commodityMapper.selectCommoditiesByOrderId(orderVo.getOrderId());
        if (orderVos.size()==orderVoList.size()){
            for (OrderVo vo : orderVoList) {
                Commodity commodity = new Commodity();
                commodity.setOrderId(vo.getOrderId());
                commodity.setCommodityNum(vo.getCommodityNum());
                commodity.setCommodityId(vo.getCommodityId());
                commodity.setProductId(vo.getProductId());
                commodityMapper.updateById(commodity);
            }
        }
        if (orderVos.size()<orderVoList.size()){
            for (int i = 0; i < orderVos.size(); i++) {
                Commodity commodity = new Commodity();
                commodity.setProductId(orderVoList.get(i).getProductId());
                commodity.setCommodityNum(orderVoList.get(i).getCommodityNum());
                commodity.setOrderId(orderVoList.get(i).getOrderId());
                commodity.setCommodityId(orderVos.get(i).getCommodityId());
                commodityMapper.updateById(commodity);
            }
            for (int i = orderVos.size(); i < orderVoList.size(); i++) {
                Commodity commodity = new Commodity();
                commodity.setProductId(orderVoList.get(i).getProductId());
                commodity.setCommodityNum(orderVoList.get(i).getCommodityNum());
                commodity.setOrderId(orderVoList.get(i).getOrderId());
                commodityMapper.insert(commodity);
            }
        }
        if (orderVos.size()>orderVoList.size()){
            for (int i = 0; i < orderVoList.size(); i++) {
                Commodity commodity = new Commodity();
                commodity.setProductId(orderVoList.get(i).getProductId());
                commodity.setCommodityNum(orderVoList.get(i).getCommodityNum());
                commodity.setOrderId(orderVoList.get(i).getOrderId());
                commodity.setCommodityId(orderVos.get(i).getCommodityId());
                commodityMapper.updateById(commodity);
            }
            for (int i = orderVoList.size(); i < orderVos.size(); i++) {
                commodityMapper.deleteById(orderVos.get(i).getCommodityId());
            }
        }
    }

    @Override
    public Order getOrderByOrderId(Integer orderId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.select("order_id","order_state");
        Order order = orderMapper.selectOne(queryWrapper);
        return order;
    }

    @Override
    public void updateOrderState(Order order) {
        orderMapper.updateById(order);
    }

    @Override
    public PageReq loadEndingOrders(Integer limit, Integer current, Integer customerId) {
        Page<OrderVo> page = new Page<>(current,limit);
        IPage<OrderVo> orders = orderMapper.selectEndingOrders(page, customerId);
        PageReq pageReq = new PageReq();
        pageReq.setMyData(orders.getRecords());
        pageReq.setTotal(orders.getTotal());
        return pageReq;
    }

    @Override
    public OrderVo getOrderByOrderIdAndCustomerId(Integer orderId, Integer customerId) {
        OrderVo orderVo = orderMapper.selectOrderInfo(orderId, customerId);
        return orderVo;
    }


}
